#!/usr/bin/env python
#coding=utf-8
import os, hashlib,re,sys
import sha 
from base64 import b64encode


def make_secret(password):
    salt = os.urandom(4)
    sha = hashlib.sha1(password)
    sha.update(salt)
    digest_salt_b64 = '{}{}'.format(sha.digest(), salt).encode('base64').strip()
    tagged_digest_salt = '{{SSHA}}{}'.format(digest_salt_b64)
    return tagged_digest_salt

def secretVerification(passwd_secret, password):
    secret_decoded = passwd_secret[6:]+'\n'
    salt = secret_decoded.decode('base64')[-4:]
    sha = hashlib.sha1(password)
    sha.update(salt)
    digest_salt_b64 = '{}{}'.format(sha.digest(), salt).encode('base64').strip()
    tagged_digest_salt = '{{SSHA}}{}'.format(digest_salt_b64)
    return tagged_digest_salt == passwd_secret

def secretVerificationMD5(passwd_secret, password):
    md = hashlib.md5()
    md.update(password)
    secret = md.digest().encode('base64').strip()
    return '%s%s'%('{MD5}',secret) == passwd_secret

def secretVerificationSHA(passwd_secret, password):
    ctx = sha.new(password) 
    hash = "{SHA}" + b64encode(ctx.digest())
    return hash == passwd_secret

def secretVerificationFactory(passwd_secret, password):
    comp = re.compile(r'\{(.*)\}')
    try:
        method = comp.findall(passwd_secret)[0]
    except:
        return False
    func = {'MD5':secretVerificationMD5, 'SSHA':secretVerification, 'SHA':secretVerificationSHA}
    return func[method](passwd_secret, password)



with open('user_list') as f:
    for i in f:
        a = i.split(' ')
        passwd_secret = a[1].strip()
        if secretVerificationFactory(passwd_secret,'secret'):
            print a[0]
'''
user_list
user1 {SSHA}sdfsdfsdfsd==
user2 {SHA}sdvcxvsdf==
user3 {md5}sdfdmcvcx=
'''